<?php

/**
 * download actions.
 *
 * @package    jobeet
 * @subpackage download
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class downloadActions extends sfActions
{
  //test
  public function executeTest(sfWebRequest $request)
  {
  	//$this->username = $this->getUser()->getUsername();
  	if(!$this->getUser()->isAuthenticated()){
  		$this->profile="yo";
  	}
  	else {
  		$this->profile=$this->getUser()->getProfile()->getUserId();
  		
  	}
  	
  }
  //search
  public function executeSearch(sfWebRequest $request){
        // returns true or false wether solr is available or not
    $solrAvailable = Doctrine_Core::getTable('Fily')->isSearchAvailable();

    if(!$solrAvailable)
      throw new sfException('La recherche n\'est pas disponible. Désolé');

    if($request->getParameter('q'))
        $this->results = Doctrine::getTable('Fily')->createSearchQuery($request
                         ->getParameter('q'))->execute();
    else
        $this->results = array();

    // returns every indexed elements
    $this->all_results = Doctrine::getTable('Fily')->createSearchQuery('*:*')
                         ->execute();

    // search in every text fields
    $this->bernard_results = Doctrine::getTable('Fily')->createSearchQuery('Bernard')
                         ->execute();

    // search only in "title" field
    $this->accent_results = Doctrine::getTable('Fily')
                         ->createSearchQuery('label_t:Lén')->execute();

    // You can set the offset and limit params
    $this->one_bernard_results = Doctrine::getTable('Fily')
                         ->createSearchQuery('bernard', 0, 1)->execute();
	}
  
	//index
  public function executeIndex(sfWebRequest $request)
  {
  	$this->categories = Doctrine_Core::getTable('Category')->getWithFilys_Day("2010","01","01","30");
  	  
  }
  
  
  public function executeDay(sfWebRequest $request)
  {
  	$this->categories = Doctrine_Core::getTable('Category')->getWithFilys_Day("y","m","d","0");
  }
  
  public function executeWeek(sfWebRequest $request)
  {
  	$this->categories = Doctrine_Core::getTable('Category')->getWithFilys_Day("y","m","d","7");
  }
  
  public function executeMonth(sfWebRequest $request)
  {
  	$this->categories = Doctrine_Core::getTable('Category')->getWithFilys_Day("y","m","1","0");
  }
  
  
  //show
  public function executeShow(sfWebRequest $request)
  {
    $this->fily = $this->getRoute()->getObject();
   
    $this->getUser()->addDownloadToHistory($this->fily);
   
  }

  public function executeNew(sfWebRequest $request)
  {
  	$fily = new Fily();
  	
  	//$link = new Link();
  	//$fily=6;
  	$fily->setLanguage('vf');
  	//$fily->setLinks($link);
  	$link=new link();
  	$this->getUser()->setAttribute("link", $link);
    
  	//$link->user_link_id=$this->getUser()->getProfile()->getId();
  	$this->getUser()->setAttribute("id", $this->getUser()->getProfile()->getUserId());
    //$this->getUser()->getAttributeHolder()->remove('link');
  	$this->form = new FilyForm($fily);
    //$form->SetValue("user_link_id");
    
    //$fily->Links[0]->user_link_id=$this->getUser()->getProfile()->getId();
  }
  

  public function executeCreate(sfWebRequest $request)
  {
    

    $this->form = new FilyForm();
	
    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {	
  	  $fily=$this->getRoute()->getObject();
  	  	
  	  $this->getUser()->setAttribute("link", $this->getRoute()->getObject()->Links[0]);
    
  	  //echo $this->getRoute()->getObject()->Links[0]->getUrl();
  	  $this->form = new FilyForm($fily);
  }
  

  

  public function executeUpdate(sfWebRequest $request)
  {
    $this->form = new FilyForm($this->getRoute()->getObject());
 	$this->processForm($request, $this->form);
  	$this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
	$request->checkCSRFProtection();
 
  	$fily = $this->getRoute()->getObject();
 	$fily->delete();
 
 	$this->redirect('download/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind(
    $request->getParameter($form->getName()),
    $request->getFiles($form->getName())
  	);
 
  	if ($form->isValid())
  	{
    $fily = $form->save();
 
    $this->redirect('download_show', $fily);
  	}
}

public function executePublish(sfWebRequest $request)
{
  $request->checkCSRFProtection();
 
  $fily = $this->getRoute()->getObject();
  $fily->publish();
 
  $this->getUser()->setFlash('notice', sprintf('Your job is now online'));
 
  $this->redirect('download_show', $fily);
}

}
